import { defineStore } from "pinia";
import router from "@/router";
import type { RouteLocationNormalizedLoaded } from "vue-router";
import { ClassTag } from "#/index";

export const useTagsStore = defineStore({
    id: "tagsStore",
    persist: true,//持久化
    state: () => ({
        tagList: [new ClassTag("/index/home/", "home", "home")]//打开的标签列表
    }),
    actions: {
        /** 
        * 添加标签
        * @params route 路由对象
        * @params index 要添加的位置，默认添加到末尾
        **/
        pushTag(route: RouteLocationNormalizedLoaded, index: number = -1) {
            const { fullPath, meta, name } = route
            if ("redirect" === name) {//过滤掉重定向页面
                return
            }
            let openTags = this.tagList;
            let findTag = openTags.find((p: ClassTag) => p.fullPath == fullPath)
            if (!findTag) {
                var i = index > -1 ? index : openTags.length
                openTags.splice(i, 0, new ClassTag(fullPath, meta.title as string, name as string))
            }
        },

        /** 
        * 删除标签
        * @params index 标签索引位置
        **/
        removeTag(index: number) {
            let openTags = this.tagList
            let tags = openTags[index]
            let route = router.currentRoute.value//当前路由
            if (route.fullPath == tags.fullPath) {
                const { fullPath } = (openTags[index + 1] || openTags[index - 1]);
                router.push(fullPath)
            }
            openTags.splice(index, 1)
        },

        /** 
        * 根据路径删除标签
        * @params fullPath 要删除的路路径
        **/
        removeTagByPath(fullPath: string) {
            let openTags = this.tagList
            let index = openTags.findIndex(item => item.fullPath == fullPath)
            if (-1 != index) {
                this.removeTag(index);//异步调用actions的removeTag
            }
        },

        /** 
        * 点击标签
        * @params index 标签索引位置
        **/
        clickTag(index: number) {
            const { fullPath } = this.tagList[index]
            let route = router.currentRoute.value;//当前路由
            if (fullPath != route.fullPath) {
                router.push(fullPath)
            }
        },

        /** 
        * 关闭所有
        **/
        closeAll() {
            this.tagList.splice(1);
            router.push({ name: "home" })
        },

        /** 
        * 删除索引所在的右侧标签
        * @params index 标签索引位置
        **/
        closeRight(index: number) {
            let openTags = this.tagList
            let activeIndex = openTags.findIndex(item => item.fullPath === router.currentRoute.value.fullPath)
            openTags.splice(index + 1)
            if (activeIndex > index) {//当前页面被删除，显示右击菜单
                const { fullPath } = openTags[index]
                router.push(fullPath)
            }
        },

        /** 
       * 刷新
       * @params index 标签索引位置
       **/
        refresh(index: number) {
            const { fullPath } = this.tagList[index]
            router.push("/index/redirect/" + fullPath)
        }
    }
})